<template>
  <div class="shop">
    <div class="nav">
      <div class="nav-content">
        <h2>产品分类</h2>
        <div style="position: relative">
          <el-col :span="12">
            <el-menu
              unique-opened
              active-text-color="#0063d1"
              default-active="1"
              class="el-menu-vertical-demo"
              @open="handleOpen"
              @close="handleClose"
            >
              <el-submenu
                :index="item.gid"
                style="width: 100%"
                v-for="item in brandList"
                :key="item.gid"
              >
                <template slot="title">
                  <div @click="changegid(item.gid)">
                    <i :class="item.icon"></i>
                    <span>{{ item.gname }}</span>
                  </div>
                </template>
                <el-menu-item-group>
                  <el-menu-item
                    @click="changebid(chil.bid)"
                    :index="chil.id"
                    v-for="chil in item.children"
                    :key="chil.id"
                    >{{ chil.gname }}</el-menu-item
                  >
                </el-menu-item-group>
              </el-submenu>
            </el-menu>
            <div class="price">
              <h2>价格筛选</h2>
              <div>
                价格区间:<input type="text" v-model="min" /><span>------</span
                ><input type="text" v-model="max" />
              </div>
              <button @click="pricefilter">筛选</button>
            </div>
          </el-col>
        </div>
      </div>
    </div>
    <div class="list-container">
      <shop-list :list="list"></shop-list>
    </div>
  </div>
</template>
<script>
import shopList from '@/components/shopList';
import { getgoodscategory, getbytype } from '@/api';
import { Toast } from 'vant';
export default {
  data: () => {
    return {
      gid: 1,
      tabPosition: 'left',
      rate: 5,
      brandList: [],
      list: [],
      templist: [],
      min: '',
      max: '',
    };
  },
  components: { shopList },
  methods: {
    pricefilter() {
      if (this.min == '' || this.max == '') {
        Toast({
          message: '请输入价格区间',
          position: 'top',
        });
        return;
      }
      this.list = this.templist;
      this.list = this.list.filter((item) => {
        return (
          parseInt(item.price) >= parseInt(this.min) &&
          parseInt(item.price) <= parseInt(this.max)
        );
      });
    },

    changegid(id) {
      this.min = this.max = '';
      this.gid = id;
      getbytype({ gid: this.gid }).then((res) => {
        this.list = res.data.data;
        this.templist = JSON.parse(JSON.stringify(this.list));
        console.log(this.list);
      });
    },
    changebid(id) {
      this.min = this.max = '';
      console.log(this.list);
      this.list = this.templist;
      this.list = this.list.filter((item) => {
        return parseInt(item.bid) == parseInt(id);
      });
    },
  },
  created() {
    // this.getgoodscategory;
    getgoodscategory().then((res) => {
      console.log(res.data.data);
      this.brandList = res.data.data;
    });
    getbytype({ gid: 1 }).then((res) => {
      this.list = res.data.data;
      this.templist = JSON.parse(JSON.stringify(this.list));
      console.log(this.list);
    });
  },
};
</script>
<style lang="scss" scoped>
.el-col {
  width: 100%;
}
.price {
  margin-top: 20px;
  button {
    height: 40px;
    width: 80%;
    background-color: #0063d1;
    border-radius: 10px;
    color: white;
    font-size: 16px;
    margin-left: 10%;
    margin-top: 20px;
  }
  div {
    font-size: 16px;
    font-weight: 600;
  }
  input {
    width: 50px;
    height: 30px;
    border-radius: 5px;
    color: orangered;
  }
}
.shop {
  position: relative;
  width: 1200px;
  height: 1600px;
  // background: pink;
  margin: 16px auto 0;

  .nav {
    position: absolute;
    width: 248px;
    height: 1600px;
    padding: 0 14px;
    left: 0;
    background: #eee;

    h2 {
      font-size: 20px;
      margin: 0 0 20px;
    }

    .nav-item {
      display: block;
      width: 80%;
      height: 32px;
      margin: 12px auto;
      border-bottom: 1px solid #999;
      cursor: pointer;

      &:hover {
        color: rgb(77, 99, 210);
      }
    }

    .slider-box {
      overflow: hidden;
      // background: purple;
      transition: height 2s;

      .slider-item {
        display: block;
        width: 66%;
        height: 20px;
        margin: 0 auto;
        line-height: 20px;
        border-bottom: 1px solid #999;
        cursor: pointer;

        &:hover {
          color: rgb(77, 99, 210);
        }
      }
    }

    .el-slider {
      width: 220px;
    }

    .compare-card {
      height: 90px;
      margin: 10px;

      .compare-preview {
        img {
          display: block;
          height: 68px;
        }
      }

      .compare-desc {
        padding-left: 12px;

        .el-rate {
          width: 100%;
          flex-wrap: nowrap;
        }
      }

      display: flex;
    }

    .tags-container {
      display: flex;
      flex-wrap: wrap;
    }
  }

  .list-container {
    position: absolute;
    width: 960px;
    right: 0;
  }
}
</style>
